﻿function HitResult(hit, x, y, element) {
    this.isHit = hit;
    this.offsetX = x;
    this.offsetY = y;
    this.element = element;
}

var imageMap = {
    "empty": "resource/images/empty.png",
    "log": "resource/images/log.png",
    "timer": "resource/images/timer.png",
    "groupStart": "resource/images/groupStart.png",
    "groupEnd": "resource/images/groupEnd.png",
    "sleep": "resource/images/sleep.png",
    "read": "resource/images/readFileActivity_48x48.png",
    "collapse": "resource/images/collapse.png",
    "expand": "resource/images/group.png",
    "default_16": "resource/images/groupStart.png",
    "default_48": "resource/images/timer.png",
    "faultHandlers": "resource/images/failPoint32.gif",
    "catchImg": "resource/images/catch_16x16.png",
    "catchAll": "resource/images/catch_all_16x16.png",
    "eventHandlersLeftImg": "resource/images/onEvent32.gif",
    "onEventLeftImg": "resource/images/onEvent32.gif",
    "onMessage": "resource/images/MEP_out_in_16x16.png",
    "catchLeftImg": "resource/images/catch32.gif",
    "catchAllLeftImg": "resource/images/catchAll32.gif",
    "stop": "resource/images/ConstructorOperation_square.jpg",
    "unpin": "resource/images/unpin.gif",
    "menu": "resource/images/menu_16x16.png",
    "pick": "resource/images/pick.gif",
    "group": "resource/images/group.png",
    "reply": "resource/images/timer.png",
    "write": "resource/images/writeFileActivity_48x48.png",
    "uncollapse": "resource/images/uncollapse.png",
    "compensationhandler": "resource/images/compensation_handler_16x16.png",
    "invoke": "resource/images/invokeActivity_48x48.png",
    "reply": "resource/images/replyActivity_48x48.png",
    "onMessageStart": "resource/images/groupStart.png",
    "onMessageEnd": "resource/images/groupEnd.png",
}

function createSvgDom(type, params) {
    var el = null;
    if (type == "Link") {
        el = new Link(params.from, params.to, params.points);
        if (params.text != undefined) {
            var p = JSON.parse(params.points);
            var fx = p[0][0];
            var lx = p[p.length - 1][0];
            var text = new Text({ x: -(lx - fx) / 2, y: 20 }, { w: 0, h: 0 }, params.text);
            el.addChildren(text);
            el = { parent: el, text: text };
        }
    }
    return el;
}

function xmlNode2WfElement(wfDiagram, parentElement, xmlNode, xmlElement) {
    var element = null;
    var xmlTagName = $(xmlNode).get(0).tagName.split(':')[1];

    var position = { x: parseInt($(xmlNode).attr("x")), y: parseInt($(xmlNode).attr("y")) };
    var size = { w: parseInt($(xmlNode).attr("width")), h: parseInt($(xmlNode).attr("height")) };

    if (xmlTagName == "process") {
        element = new ProcessContainer(position, size, "white", { id: $(xmlNode).attr("id"), r: 5, color: "rgb(255,233,130)", text: $(xmlNode).attr("name"), svgHeight: parseInt($(xmlNode).attr("height")) + 50 });
        processEl = element;
    }
    else if (xmlTagName == "scope") {
        if ($(xmlNode).attr("x") != undefined) {
            element = new ScopeContainer(position, size, "white", { r: 15, expand: $(xmlNode).attr("collapse") });
        }
    }
    else if (xmlTagName == "pick") {
        if ($(xmlNode).attr("x") != undefined) {
            element = new PickContainer(position, size, "white", { r: 15, expand: $(xmlNode).attr("collapse") });
        }
    }
    else if (xmlTagName == "catch") {
        if ($(xmlNode).attr("x") != undefined) {
            var catchText = $(xmlNode).attr("faultName");
            if (catchText == undefined) {
                catchText = "";
            } else {
                catchText = catchText.split(':')[1];
            }
            element = new CatchContainer(position, size, "white", { r: 4, text: catchText, color: "transparent", leftImage: imageMap["catchLeftImg"], leftTopImg: imageMap["catchImg"], expand: $(xmlNode).attr("collapse") });
        }
    } else if (xmlTagName == "catchAll") {
        if ($(xmlNode).attr("x") != undefined) {
            element = new CatchContainer(position, size, "white", { r: 4, text: "Exception", color: "transparent", leftImage: imageMap["catchAllLeftImg"], leftTopImg: imageMap["catchAll"], expand: $(xmlNode).attr("collapse") });
        }
    }
    else if (xmlTagName == "onMessage") {
        if ($(xmlNode).attr("x") != undefined) {
            var operation = $(xmlNode).attr("operation");
            var partnerLink = $(xmlNode).attr("partnerLink");
            var text = "";
            if (operation != undefined && partnerLink != undefined) {
                text = partnerLink + "/" + operation;
            }
            element = new OnMessageContainer(position, size, "white", { r: 4, text: text, color: "transparent", leftImage: imageMap["groupStart"], rightImage: imageMap["groupEnd"], expand: $(xmlNode).attr("collapse"), tag: partnerLink + "_" + operation });
        }
    }
    else if (xmlTagName == "compensationHandler") {
        if ($(xmlNode).attr("x") != undefined) {
            element = new CompensationHandlerContainer(position, size, "white", { r: 15, leftRx: 4, color: "transparent", dasharray: 7, text: "Compensation", expand: $(xmlNode).attr("collapse") });
        }
    }
    else if (xmlTagName == "onEvent") {
        if ($(xmlNode).attr("x") != undefined) {
            var operation = $(xmlNode).attr("operation");
            var partnerLink = $(xmlNode).attr("partnerLink");
            var text = "";
            if (operation != undefined && partnerLink != undefined) {
                text = partnerLink + "/" + operation;
            }
            element = new OnEventContainer(position, size, "white", { r: 4, text: text, color: "transparent", leftImage: imageMap["onEventLeftImg"], expand: $(xmlNode).attr("collapse") });
        }
    }
    else if (xmlTagName == "extensionActivity" || xmlTagName == "empty" || xmlTagName == "reply" || xmlTagName == "write" || xmlTagName == "invoke") {
        var tempImg = imageMap[$(xmlNode).attr("type")];
        if (tempImg == undefined) {
            tempImg = imageMap["groupStart"];
        }
        element = new Image(position, size, tempImg,   $(xmlNode).attr("partnerLink") + "_" + $(xmlNode).attr("name") );
    }
    else if (xmlTagName == "link") {
        if ($(xmlNode).attr("points") != undefined) {
            if ($(xmlNode).attr("tibex:label") == undefined) {
                element = createSvgDom("Link", { from: parentElement, to: parentElement, points: $(xmlNode).attr("points") });
            }
            else {
                element = createSvgDom("Link", { from: parentElement, to: parentElement, points: $(xmlNode).attr("points"), text: $(xmlNode).attr("tibex:label") });
                wfDiagram.addElement(element.text);
                element = element.parent;
            }
        }
    }

    if (element != null) {
        var children = $(xmlNode).children();
        if (children != null) {
            children.each(function () {
                xmlNode2WfElement(wfDiagram, element, $(this), xmlElement);
            })
        }
        if (parentElement != null) {
            parentElement.addChildren(element);
            wfDiagram.addElement(element);
        }
        else {
            wfDiagram.addElement(element);
        }
    }
    else {
        var children = $(xmlNode).children();
        if (children != null) {
            children.each(function () {
                xmlNode2WfElement(wfDiagram, parentElement, $(this), xmlElement);
            })
        }
    }
}